<template>
  <div>
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @confirm="onConfirm" @cancel="$emit('close')" />
  </div>
</template>

<script>
import { updataUsersAPI } from '@/api/User.js'
import dayjs from 'dayjs'
export default {
  name: 'updatabirthday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, //禁止背景点击
        duration: 0
      })
      try {
        const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
        await updataUsersAPI({
          birthday: currentDate
        })
        // 更新视图
        this.$emit('input', currentDate)
        //关闭弹出层
        this.$emit('close')
        //提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('修改生日失败，请稍后重试！')
        console.log(err)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
